package edu.mis.model;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;



public class Cart {
    protected Map<Integer,OrderDetail> items;
    private double totalPrice;
    public Cart(){
        if(items==null)	{
            items=new HashMap<Integer,OrderDetail>();
        }
    }

    public void addCommodity(Integer commodityId,OrderDetail orderitem){
        if(items.containsKey(commodityId)){
            OrderDetail _orderitem=items.get(commodityId);
            _orderitem.setQty(_orderitem.getQty()+orderitem.getQty());
            items.put(commodityId, _orderitem);

        }
        else{
            items.put(commodityId, orderitem);
        }
    }
    public void clearCart(){
        items.clear();
    }
    public void removeCommodity(Integer commodityId){
        items.remove(commodityId);
    }
    public void updateCart(Integer commodityId,int qty){
        OrderDetail orderitem=items.get(commodityId);
        orderitem.setQty(qty);
        items.put(commodityId, orderitem);
    }

    public double getTotalPrice(){
        int totalPrice=0,qty;
        for(Iterator it=items.values().iterator();it.hasNext();){
            OrderDetail orderitem=(OrderDetail)it.next();
            Commodity commodity=orderitem.getCommodity();
            qty=orderitem.getQty();
            totalPrice+=commodity.getPrice()*qty;

        }
        return totalPrice;
    }

    public Map<Integer, OrderDetail> getItems() {
        return items;
    }

    public void setItems(Map<Integer, OrderDetail> items) {
        this.items = items;
    }
}
